package com.len.controller;

import com.len.base.BaseController;
import com.len.entity.SpGoods;
import com.len.service.SpGoodsService;
import com.len.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static cn.hutool.core.util.StrUtil.uuid;

@Controller
@RequestMapping("goods")
public class SpGoodsController extends BaseController<SpGoods> {
    @Autowired
    private SpGoodsService spGoodsService;
    @Autowired
    UploadUtil uploadUtil;
    @GetMapping(value = "/showGoods")
    public String showUser(Model model) {
        return "/goods/goodsList";
    }

    @GetMapping(value = "showGoodsList")
    @ResponseBody
    public ReType showUser(Model model, SpGoods spGoods, String page, String limit) {
        return spGoodsService.show(spGoods, Integer.valueOf(page), Integer.valueOf(limit));
    }

    @GetMapping(value = "/showAddGoods")
    public String showAddGoods(Model model) {
        return "/goods/add-goods";
    }

    @GetMapping("/update")
    public String update(String id, Model model, boolean detail){
        SpGoods spGoods = spGoodsService.selectByPrimaryKey(id);
        model.addAttribute("spGoods",spGoods);
        model.addAttribute("detail", detail);
        return "/goods/update-goods";
    }
    @GetMapping("/detail")
    public String detail(String id, Model model, boolean detail){
        SpGoods spGoods = spGoodsService.selectByPrimaryKey(id);
        model.addAttribute("spGoods",spGoods);
        model.addAttribute("detail", detail);
        return "/goods/goods-detail";
    }
    @RequestMapping("/updateData")
    @ResponseBody
    public JsonUtil updateData(SpGoods spGoods, Model model, boolean detail){
        JsonUtil jsonUtil = new JsonUtil();
        //获取旧数据
        SpGoods spGoodsOld = spGoodsService.selectByPrimaryKey(spGoods.getId());
        //复制
        BeanUtil.copyNotNullBean(spGoods, spGoodsOld);
        //更新
        spGoodsService.updateByPrimaryKeySelective(spGoodsOld);
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }
    @RequestMapping("save1")
    @ResponseBody
    public JsonUtil save(SpGoods spGoods){
        JsonUtil jsonUtil = new JsonUtil();
        try {
            spGoods.setId(uuid());
            spGoods.setTbGoodsStatus("1");
            spGoods.setTbGoodsNum("1");
            spGoods.setTbGoodsAddtime(CommonUtil.getDate1());
            spGoodsService.insert1(spGoods);
        }catch (Exception e){
            e.printStackTrace();
        }
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }
    @RequestMapping("/showGood")
    @ResponseBody
    public Map show(String id){
        Map map = null;
        try {
            map = spGoodsService.selectId2(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
    /**
     * 头像上传 目前首先相对路径
     */
    @PostMapping(value = "upload")
    @ResponseBody
    public JsonUtil imgUpload(HttpServletRequest req, @RequestParam("file") MultipartFile file,
                              ModelMap model) {
        String fileName = uploadUtil.upload(file);
        JsonUtil j = new JsonUtil();
        j.setMsg(fileName);
        return j;
    }

    /**
     * 获取分区商品
     * @param type
     * @return
     */
    @RequestMapping("getTypeGoods")
    @ResponseBody
    public List<Map> getType (String type){
        String date = CommonUtil.getDate2();
//        List<Map> maps = spGoodsService.getProductByType(date,type);
        return spGoodsService.getProductByType(date,type);
    }
    /**
     * 获取分区
     * @param type
     * @return
     */
    @RequestMapping("goodsType")
    public String goodsType (String type,Model model){
        model.addAttribute("type",type);
        return "/shop/productDetails";
    }
}
